package com.rp.system.dao;

import java.util.List;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.springframework.stereotype.Repository;

import com.rp.system.po.MenuItem;

@Repository
public class MenuItemDao extends DbaHibernateTemplateSupport<MenuItem> {
	
	public static final String INSERT_MENUITEM = "INSERT INTO RP_SYSTEM_MENUITEMS(id,createDate,updateDate,version,menuId,text,url,pid,iconCls,sort,verifyClose) VALUES(?,?,?,?,?,?,?,?,?,?,?)";

	public List<MenuItem> getMenuItemsByMenuId(String menuId) {
		String hql = "from MenuItem where menuId = ? and enabled2 = '1'" + DbOperUtil.generateOrderBy("sort", "asc");
		return this.find(hql, menuId);
	}
	
	public List<MenuItem> getMenuItemsByMenuName(String menuName){
		String hql = "from MenuItem where menuName = ? and enabled2 = '1'" + DbOperUtil.generateOrderBy("sort", "asc");
		return this.find(hql, menuName);
	}
	
	/**
	 * 根据菜单ID获取菜单下的全部菜单项
	 * @param menuId
	 * @return
	 */
	public List<MenuItem> getAllMenuItemsByMenuId(String menuId){
		String hql = "from MenuItem where menuId = ?" + DbOperUtil.generateOrderBy("sort", "asc");
		return this.find(hql, menuId);
	}
	
	/**
	 * 根据显示名称获取菜单项
	 * @param text
	 * @return
	 */
	public MenuItem getMenuItemByText(String text){
		String hql = "from MenuItem where text = ?";
		List<MenuItem> mis = this.find(hql, text);
		if(mis.size() > 0){
			return mis.get(0);
		}
		return null;
	}
	
}
